package com.bigdata.appliance.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bigdata.appliance.common.exception.BusinessException;
import com.bigdata.appliance.common.exception.ResponseCode;
import com.bigdata.appliance.common.pojo.PageResult;
import com.bigdata.appliance.entity.Cart;
import com.bigdata.appliance.mapper.CartMapper;
import com.bigdata.appliance.service.ICartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bigdata.appliance.vo.req.AddCartReqVO;
import com.bigdata.appliance.vo.req.DeleteCartReqVO;
import com.bigdata.appliance.vo.req.PageCartReqVO;
import com.bigdata.appliance.vo.resp.PageCartRespVO;
import com.bigdata.appliance.vo.resp.PageOrdersRespVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 购物车表 服务实现类
 * </p>
 *
 * @author appliance-shop
 * @since 2023-12-24
 */
@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements ICartService {

    @Resource
    private CartMapper cartMapper;

    @Override
    public void addCarts(AddCartReqVO vo) {
        Cart cart = new Cart();
        cart.setUserId(vo.getUserId());
        cart.setGoodsId(vo.getGoodsId());
        int result = cartMapper.insert(cart);
        if (result !=1){
            throw new BusinessException(ResponseCode.OPERATION_ERROR.getCode(), ResponseCode.OPERATION_ERROR.getMessage());
        }
    }

    @Override
    public void deleteCarts(DeleteCartReqVO vo) {
        int result = cartMapper.deleteById(vo.getId());
        if (result !=1){
            throw new BusinessException(ResponseCode.OPERATION_ERROR.getCode(), ResponseCode.OPERATION_ERROR.getMessage());
        }
    }

    @Override
    public PageResult<PageCartRespVO> pageCart(PageCartReqVO vo) {
        IPage<PageCartRespVO> result = cartMapper.selectPageCarts(new Page<>(vo.getCurrent(), vo.getSize()), vo);
        return PageResult.getPage(result.getRecords(),result.getTotal());
    }
}
